import { log } from "@/contrib";
import { updateCreepData } from "@/contrib/creep/utils";
import LoopObject from "@/loop";
import { SepicalBodyType } from "@/modulesRoom/taskWork/types";
import { CreepConfig, CreepData, CreepRole, RoleCreep, RoleCreepMemory, RoleDatas } from "@/role/types/role";
import creep from "../creep";
import CreepNumberSettings from "../creep/numberSettings";
import room from "../room";
import { creepDefaultMemory } from "./constant";
import { GetName } from "./nameGetter";
import { CreepWork, roles } from "./spawnRoles";
import { MySpawnReturnCode, SpawnTask } from "./types";

export default class SpawnControlLoopObject extends LoopObject {

    tickStart(): void {

    }

    spawnCheck(room: Room, roleName: CreepRole, num: number, force: boolean = false): number | boolean {
        const creeps: Creep[] = []
        for (const name in Game.creeps) {
            const creep = Game.creeps[name]
            if (creep.memory.spawnRoom == room.name && creep.memory.role == roleName) {
                creeps.push(creep)
            }
        }
        if ((creeps.length < num) || (creeps.length < num + 1 && force)) {
            return creeps.length
        }
        return false
    }

    do(): void {

        //
        // 爬爬数量配置
        //
        CreepNumberSettings.init()

        // 遍历所有 creep 内存，检查其是否存在
        for (const name in Memory.creeps) {
            if (name in Game.creeps) continue
            delete Memory.creeps[name]
        }

        const spawns = _.filter(Game.spawns, (spawnItem) => spawnItem.owner && spawnItem.owner.username == 'noahw')

        Object.values(spawns).forEach(spawnItem => {

            if (!(spawnItem.owner.username == 'noahw')) return

            const spawn: StructureSpawn = spawnItem

            if (!spawn) return

            // const roomLevel = spawn.room.controller.level

            // Object.values(Game.spawns).forEach(spawn => {
            //     this.runSpawnTask(spawn)
            // CreepNumberSettings.runSpawnTask(spawn)
            // })

            const harvesterNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.Harvester)
            const managerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.Manager)
            const workerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.Worker)
            const remoteManagerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.RemoteManager)
            const remoteHarvesterNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.RemoteHarvester)
            const wallFillerNumNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.WallerFiller)
            const upgraderNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.Upgrader)
            const processorNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.Processor)
            const centerManagerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.CenterManager)
            const reserverNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.Reserver)
            const attackerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.Attacker)
            const remoteWorkerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.RemoteWorker)
            const centerLabManagerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.CenterLabManager)
            const taskManagerNumSetting = CreepNumberSettings.spawnCheck(spawn.room.name, CreepRole.TaskManager)

            if (reserverNumSetting && !reserverNumSetting.needSpawn && reserverNumSetting.creepsNum == 1) {
                const reserverCreepId = Game.rooms['E35S48'].memory.reserverCreepId
                const reserverCreep = Game.getObjectById<Creep>(reserverCreepId) as Creep
                if (reserverCreep) {
                    if (reserverCreep.ticksToLive < 100) {
                        reserverNumSetting.needSpawn = true
                    }
                }
            }

            if (Game.time % 5 == 0) {

                // spawn.room.controller
                if (spawn.name == 'Spawn1' || spawn.name == 'Spawn2' || spawn.name == 'Spawn4' || spawn.name == 'Spawn6') {

                    spawn.room.log(
                        ' P:' + (processorNumSetting ? processorNumSetting.creepsNum : 0) +
                        ' TM:' + (taskManagerNumSetting ? taskManagerNumSetting.creepsNum : 0) +
                        ' H:' + (harvesterNumSetting ? harvesterNumSetting.creepsNum : 0) +
                        ' M:' + (managerNumSetting ? managerNumSetting.creepsNum : 0) +
                        ' W:' + (workerNumSetting ? workerNumSetting.creepsNum : 0) +
                        ' U:' + (upgraderNumSetting ? upgraderNumSetting.creepsNum : 0) +
                        ' CM:' + (centerManagerNumSetting ? centerManagerNumSetting.creepsNum : 0) +
                        ' WF:' + (wallFillerNumNumSetting ? wallFillerNumNumSetting.creepsNum : 0) +
                        ' RH:' + (remoteHarvesterNumSetting ? remoteHarvesterNumSetting.creepsNum : 0) +
                        ' RM:' + (remoteManagerNumSetting ? remoteManagerNumSetting.creepsNum : 0) +
                        ' RW:' + (remoteWorkerNumSetting ? remoteWorkerNumSetting.creepsNum : 0) +
                        ' RS:' + (reserverNumSetting ? reserverNumSetting.creepsNum : 0) +
                        ' A:' + (attackerNumSetting ? attackerNumSetting.creepsNum : 0)
                    )
                }
            }

            if (spawn.spawning) return

            // CreepNumberSettings.doSpawnTask()

            const processorName = GetName.processor(spawn.room.name)
            const processorCreep = Game.creeps[processorName]
            if (processorCreep && processorCreep.ticksToLive) {
                const ticksToLife = processorCreep.ticksToLive
                if (ticksToLife <= 100 && ticksToLife >= 1) {
                    return
                }
                if (ticksToLife < 1) {
                    //
                    // Processor
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Processor, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] Processor' + status)
                }
            }

            if (processorNumSetting && processorNumSetting.needSpawn) {
                //
                // Processor 临时
                const data: CreepData = {
                    workRoom: spawn.room.name,
                }
                const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Processor, data)
                console.log(spawn.room.name + ' [doSpawnCreep] Processor' + status)
            } else
                if (harvesterNumSetting && harvesterNumSetting.needSpawn) {
                    //
                    // Harvester
                    if (spawn.room['source']) {

                        let harvesterIndex
                        let num: number = CreepNumberSettings._data[spawn.room.name][CreepRole.Harvester]
                        for (var i = 0; i < num + 1; i++) {
                            if (!Game.creeps[CreepNumberSettings.getName(CreepRole.Harvester, spawn.room.name, i)]) {
                                harvesterIndex = i
                                break
                            }
                        }

                        let sourceId
                        for (const id in spawn.room.memory.source) {
                            if (harvesterIndex == spawn.room.memory.source[id].index) {
                                sourceId = id
                            }
                        }

                        if (!sourceId || !Game.getObjectById(sourceId)) {
                            return
                        }

                        if (harvesterIndex === undefined) {
                            return
                        }

                        if (sourceId) {
                            const data: CreepData = {
                                harvestRoom: spawn.room.name,
                                useRoom: spawn.room.name,
                                sourceId: sourceId,
                                workRoom: spawn.room.name,
                            }

                            if (spawn.room[STRUCTURE_CONTAINER][0]) {
                                const targetId = spawn.room[STRUCTURE_CONTAINER][0].id
                                data['targetId'] = targetId
                            }
                            const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Harvester, data)
                            console.log(spawn.room.name + ' [doSpawnCreep] Harvester' + status)
                        }
                    }
                }
                else if (processorNumSetting && processorNumSetting.needSpawn) {
                    //
                    // Processor 临时
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Processor, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] Processor' + status)
                }
                else if (managerNumSetting && managerNumSetting.needSpawn) {
                    //
                    // Manager
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Manager, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] Manager' + status)
                }
                else if (attackerNumSetting && attackerNumSetting.needSpawn) {
                    //
                    // Attacker
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Attacker, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] Attacker' + status)
                }
                else if (centerManagerNumSetting && centerManagerNumSetting.needSpawn) {
                    //
                    // CenterManagerNum
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.CenterManager, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] CenterManager' + status)
                }
                else if (workerNumSetting && workerNumSetting.needSpawn) {
                    //
                    // Worker
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Worker, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] Worker' + status)
                }
                else if (upgraderNumSetting && upgraderNumSetting.needSpawn) {
                    //
                    // Upgrader
                    let data
                    if (spawn.room.controller.level == 7) {
                        data = {
                            workRoom: spawn.room.name,
                            bodyType: SepicalBodyType.Upgrade7,
                        }
                    }
                    else if (spawn.room.controller.level == 8) {
                        data = {
                            workRoom: spawn.room.name,
                            bodyType: SepicalBodyType.Upgrade8,
                        }
                    }
                    else {
                        data = {
                            workRoom: spawn.room.name,
                        }
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Upgrader, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] Upgrader' + status)
                }
                else if (remoteManagerNumSetting && remoteManagerNumSetting.needSpawn) {
                    //
                    // RemoteManager
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.RemoteManager, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] RemoteManager' + status)
                }
                else if (wallFillerNumNumSetting && wallFillerNumNumSetting.needSpawn) {
                    //
                    // WallerFiller
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.WallerFiller, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] WallerFiller' + status)
                }
                else if (remoteHarvesterNumSetting && remoteHarvesterNumSetting.needSpawn) {
                    //
                    // RemoteHarvester
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.RemoteHarvester, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] RemoteHarvester' + status)
                }
                else if (reserverNumSetting && reserverNumSetting.needSpawn) {
                    //
                    // Reserver
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Reserver, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] Reserver' + status)
                }
                else if (remoteWorkerNumSetting && remoteWorkerNumSetting.needSpawn) {
                    //
                    // RemoteWorker
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.RemoteWorker, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] RemoteWorker' + status)
                }
                else if (centerLabManagerNumSetting && centerLabManagerNumSetting.needSpawn) {
                    //
                    // CenterLabManager
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.CenterLabManager, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] CenterLabManager' + status)
                }
                else if (taskManagerNumSetting && taskManagerNumSetting.needSpawn) {
                    //
                    // TaskManager
                    const data: CreepData = {
                        workRoom: spawn.room.name,
                    }
                    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.TaskManager, data)
                    console.log(spawn.room.name + ' [doSpawnCreep] TaskManager' + status)
                }
        })
    }
    afterWork(): void {
        super.afterWork()
    }
    tickEnd(): void {
        super.tickEnd()
    }
}
